package cn.dengta.webapp.push.vo;

import java.io.Serializable;
import java.util.Date;
import java.util.EnumMap;
import javax.annotation.Nullable;

import cn.dengta.common.context.I18N;
import cn.dengta.common.model.Bean;
import cn.dengta.context.model.ImI18nKey;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.push.dto.UserDTO;
import cn.dengta.webapp.trade.entity.GiftLog;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 飘屏动画 数据载体
 */
@Getter
@Setter
@Accessors(chain = true)
public class EventNotifyVO implements Bean<Long> {

	protected Long id;
	protected UserDTO user;
	//	protected UserDTO to;
	protected MsgSegment[] msg;
	/** SVGA 是一种同时兼容 iOS / Android / Flutter / Web 多个平台的动画格式 */
	protected String svga = "gift" /* "common" */;
	protected Date time;

	/**
	 * 【礼物】
	 */
	public static EnumMap<Language, EventNotifyVO> of(GiftLog log) {
		EnumMap<Language, EventNotifyVO> map = new EnumMap<>(Language.class);
		for (Language language : Language.CACHE) {
			EventNotifyVO vo = new EventNotifyVO();
			vo.id = log.getId();
			String anonymousName = "xxx";
			String sendUserNickname = log.isAnonymous(true) ? anonymousName : log.getSendUserNickname();
			String toUserNickname = log.isAnonymous(false) ? anonymousName : log.getToUserNickname();
			vo.user = new UserDTO(log.getSendUserId(), sendUserNickname, log.getSendUserAvatar());
			//		this.to = new UserDTO(log.getToUserId(), log.getToUserNickname(), log.getToUserAvatar());
			//		this.gift = new GiftDTO(log.getGiftId(), log.getGiftName(), log.getGiftIcon(), log.getGiftNum(), log.getTotalCoin());
			vo.msg = new MsgSegment[] {
					MsgSegment.ofHighlight(vo.user),
					MsgSegment.of(language.msg(ImI18nKey.SEND_TO)),
					MsgSegment.ofHighlight(toUserNickname, "#fff17f"),
					MsgSegment.ofHighlight(X.expectNotNull(log.getI18nName(language), log.getGiftName(), I18N.msg(ImI18nKey.GIFT, language.locale)) + " x " + log.getGiftNum(), "#ffffff").setLink(log.getGiftIcon())
			};
			vo.time = log.getAddTime();
			map.put(language, vo);
		}
		return map;
	}

	/**
	 * 【个人VIP、群VIP】
	 *
	 * @param groupVipName 如果该参数不为空，则表示【群VIP】
	 */
	public static EnumMap<Language, EventNotifyVO> ofVip(UserDTO user, Long logId, @Nullable String groupVipName) {
		EnumMap<Language, EventNotifyVO> map = new EnumMap<>(Language.class);

		final boolean groupVIP = StringUtil.notEmpty(groupVipName);
		final Date now = new Date();
		for (Language language : Language.CACHE) {
			EventNotifyVO vo = new EventNotifyVO();
			vo.id = logId;
			vo.user = user;
			vo.msg = new MsgSegment[] {
					MsgSegment.ofHighlight(vo.user),
					MsgSegment.of(language.msg(ImI18nKey.OPEN)),
					MsgSegment.ofHighlight(groupVIP ? language.msg(ImI18nKey.GROUP_VIP, groupVipName) : language.msg(ImI18nKey.VIP))
			};
			vo.time = now;
			vo.svga = "vip";
			map.put(language, vo);
		}
		return map;
	}

	/**
	 * 【个人VIP】
	 */
	public static EnumMap<Language, EventNotifyVO> ofVip(UserDTO user, Long logId) {
		return ofVip(user, logId, null);
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Getter
	@Setter
	@Accessors(chain = true)
	public static class MsgSegment implements Serializable {

		private static final long serialVersionUID = 1L;

		protected String text;
		protected String color;
		protected String link;

		public static MsgSegment of(String text) {
			return new MsgSegment().setText(text);
		}

		public static MsgSegment ofHighlight(String text, String color) {
			return new MsgSegment().setText(text).setColor(color);
		}

		public static MsgSegment ofHighlight(String text) {
			return ofHighlight(text, "#F5CF54");
		}

		public static MsgSegment ofHighlight(UserDTO dto) {
			return ofHighlight(dto.getNickname(), "#fff17f");
		}

	}

}
